package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.OrderInfo;
import cn.wolfcode.shop.mapper.OrderInfoMapper;
import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.util.IdGenerateUtil;
import cn.wolfcode.shop.vo.GoodVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderInfoServiceImpl implements IOrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Transactional
    @Override
    public OrderInfo createOrder(Long userId, GoodVo goodVo) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(null);
        orderInfo.setGoodCount(1);
        orderInfo.setGoodId(goodVo.getGoodId());
        orderInfo.setGoodImg(goodVo.getGoodImg());
        orderInfo.setGoodName(goodVo.getGoodName());
        orderInfo.setGoodPrice(goodVo.getGoodPrice());
        orderInfo.setSeckillPrice(goodVo.getSeckillPrice());
        orderInfo.setUserId(userId);
        orderInfo.setOrderNo(""+ IdGenerateUtil.get().nextId());
        orderInfoMapper.insert(orderInfo);
        return orderInfo;
    }

    @Override
    public OrderInfo get(String orderNo) {
        return orderInfoMapper.selectByOrderNo(orderNo);
    }
}
